<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>miniMVC指导手册</title>
<style>
*{
margin:0;
padding:0;
}
body{
font-size:14px;
margin-top:20px;
}
a{
color:#03C;	
}
a:visited{
color:#C93;	
}
td{
height:30px;
line-height:30px;	
}
p,pre{
width:90%;
border:2px dotted #F33;
line-height:26px;
padding:16px;
margin-top:5px;
margin-bottom:5px;
margin-left:auto;
margin-right:auto;
font-size:16px;
}
div#top{
width:800px;
margin:0 auto;
text-align:center;	
}
span{
color:green;
font-weight:bold;
text-decoration:underline;
}
em{
font-style:normal;
color:red;	
font-weight:bold;
text-decoration:underline;
}
</style>
</head>
<body>
<div id="top">
<table width="100%" border="1" cellspacing="3" cellpadding="3">
  <tr>
    <td><a href="#link_1">入门简介</a></td>
  </tr>
  <tr>
    <td><a href="#link_2">取得配置文件信息</a></td>
  </tr>
  <tr>
    <td><a href="#link_3">控制器原型</a></td>
  </tr>
  <tr>
    <td><a href="#link_4">控制器文件示例</a></td>
  </tr>
  <tr>
    <td><a href="#link_5">控制器中包含模板</a></td>
  </tr>
  <tr>
    <td><a href="#link_6">模板顶部</a></td>
  </tr>
  <tr>
    <td><a href="#link_7">rule文件示例</a></td>
  </tr>
  <tr>
    <td><a href="#link_8">service文件示例</a></td>
  </tr>
  <tr>
    <td><a href="#link_9">模板中常用php方法</a></td>
  </tr>
  <tr>
    <td><a href="#link_10">常用全局函数</a></td>
  </tr>
  <tr>
    <td><a href="#link_11">控制器类完整示例</a></td>
  </tr>
  <tr>
    <td><a href="#link_12">服务类完整示例</a></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>封装查询关联字段<br />
封装查询排序</td>
  </tr>
</table>
</div>
<br />
<p>
<em><a name="link_1">入门简介：</a></em>
入口文件：admin.php
它包含了core/init.php,admin/project.inc.php<br />
admin文件夹下有
controllers(控制器类)<br />
exps(快速操作类)<br />
models(数据库模型类)<br />
rules(数据验证规则)<br />
runtime(运行日志文件夹)<br />
services(操作服务类)<br />
static(静态文件)<br />
viewers(视图)<br />
project.inc.php(项目配置)
</p>
<pre>
<em><a name="link_2">取得配置文件信息：</a></em>
普通文件.php $C
控制器类 $this->conf
服务类 $this->conf
</pre>
<pre>
<em><a name="link_3">控制器原型：</a></em>
class Controller{
	protected $viewer_dir=null;
	
	protected $model=null;
	protected $conf=null;
	protected $m=null;
	
	public function __construct(){
		$this->viewer_dir=VIEWER_DIR.DEFAULT_VIEWER_NAME.'/';
		$this->conf=Conf::getIns();
		$this->model=Model::getIns();
		$this->m=$this->model->getConnection();
	}
//
}
</pre>
<pre>
<em><a name="link_4">控制器文件示例：</a></em>
未登陆前：
<span>indexController.class.php</span>
class indexController extends Controller {
	public function __construct(){
		parent::__construct();
		$this->svc=load_service('index');
	}
}
登陆后：
<span>indexController.class.php</span>
class indexController extends accessController {
	public function __construct(){
		parent::__construct();
		$this->svc=load_service('index');
	}
}
<span>accessController.class.php</span>
class accessController extends Controller{
    public function __construct(){
        parent::__construct();
        //检测session
        if(!isset($_SESSION['MY'])){
            exit('请走正门！');
        }
    }
}
</pre>
<p>
<em><a name="link_5">控制器中包含模板：</a></em><br />
include($this->viewer_dir.'folder/add.php');
</p>
<p>
<em><a name="link_6">模板顶部：</a></em><br />
&lt;?php if(!defined('A')) exit;?&gt;
</p>
<pre>
<em><a name="link_7">rule文件示例：</a></em>
&lt;?php exit;?&gt;
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;form&gt;
&lt;input name="parent_id" type="notnull" action="add" msg="上级分类不能为空！" /&gt;
&lt;input name="name" type="length" value="1,50" msg="分类名称在1-50之间！" /&gt;
&lt;input name="type" type="notnull" action="add" msg="类型不能为空！" /&gt;
&lt;input name="mysort" type="notnull" msg="排序不能为空！" /&gt;
&lt;input name="isshow" type="notnull" msg="显隐不能为空！" /&gt;
&lt;/form&gt;
</pre>
<pre>
<em><a name="link_8">service文件示例：</a></em>
<span>questionService.class.php</span>
class questionService extends Service{
    public function __construct(){
        parent::__construct();
        $this->table=table('question');
    }
}
</pre>
<pre>
<em><a name="link_9">模板中常用php方法：</a></em>
loadcss(array('reset','shell'));//PROJECT_CSS_URL(项目的css)
loadcss('aaa',P_CSS);//P_CSS(公共的css)
loadjs(array('a','b'));//PROJECT_JS_URL(项目的js)
loadjs('jquery',P_JS);//(公共的js)
还有个PROJECT_IMG_URL，项目图片的url
更多可参考project.inc.php
</pre>
<pre>
<em><a name="link_10">常用全局函数：</a></em>
<span>table();//取得表名</span>
function table($name){
	$conf=Conf::getIns();
	$cfg=$conf->load_config('DB');
	return $cfg['prefix'].$name;
}
<span>s_i_p();//设置包含目录</span>
function s_i_p($path_array){
	array_unshift($path_array,get_include_path());
	set_include_path(implode(PATH_SEPARATOR,$path_array));
}
load_service();//加载项目service
alert();//弹出消息
autoalert();//根据结果提示成功或失败！
jump();//跳到指定操作下
autojump();//跳到当前操作下
back();//跳到上一个操作
reload();//刷新父页面或当前页
js();//输出一段js
url();//返回url地址，模板中常用
loadcss();//加载css
loadjs();//加载js
</pre>
<pre>
<em><a name="link_11">控制器类完整示例：</a></em>
&lt;?php
class sampleController extends accessController{
    public function __construct(){
        parent::__construct();
        $this->svc=load_service('sample');
    }
    public function add(){
        $block_id=intval($_GET['block_id']);
        if($block_id>0){
        }else{
            exit('没有指定知识块ID!');
        }
        include ($this->viewer_dir.'question/add.php');
    }
    public function add_(){
    	if($_POST){
			$r=$this->svc->add_($_POST);
			autoalert($r[0],$r[1]);
			back();
        }
    }
    public function li(){
        $block_id=intval($_GET['block_id']);
        if($block_id){
            $return=$this->svc->li(array('block_id'=>$block_id));
            include($this->viewer_dir.'question/list.php');
        }else{
            exit('没有指定知识块ID!');
        }
    }
     public function mod(){
        $id=intval($_GET['id']);
        if($id>0){
            $data=$this->svc->mod($id);
            if($data){
                include($this->viewer_dir.'question/mod.php');
            }
        }
    }
    public function mod_(){
		$id=intval($_GET['id']);
        if($_POST and $id>0){
            $r=$this->svc->mod_($_POST,$id);
            autoalert($r[0],$r[1]);
            back();
        }
    }
    public function del(){
		$id=intval($_GET['id']);
        if($id>0){
            $r=$this->svc->del($id);
            autoalert($r[0],$r[1]);
            back();
        }
	}
//
}
</pre>
<pre>
<em><a name="link_12">服务类完整示例：</a></em>
&lt;?php
class questionService extends Service{
    private $table=null;
    public function __construct(){
        parent::__construct();
        $this->table=table('question');
    }
    public function add_($post){
        $result=array();
        $vali=validate::check('question',$post,'add');
        if($vali){
            $result[0]=$this->model->_i($this->table,$post);
        }else{
            $result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
        }
        return $result;
    }
    public function mod($id){
        return $this->model->_f($this->table,$id);
    }
    public function mod_($post,$id){
        $result=array();
        $vali=validate::check('question',$post,'mod');
        if($vali){
    		$result[0]=$this->model->_u($this->table,$post,$id);
    	}else{
    		$result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
    	}
        return $result;
    }
    public function del($id){
        $return=array();
        $return[0]=$this->model->_d($this->table,$id);
        return $return;
    }
    public function li($condition=array()){
        $return=array();
        $query="select * from `{$this->table}` where `block_id`='{$condition['block_id']}' order by `mysort` asc";
        $p=new smart_pager($query,$this->m);
        $p->address('&block_id=%block_id%','block_id',$condition['block_id']);
        if($p->total_num>0){
            $return[]=$this->m->mselect($p->query);
            $return[]=$p;
        }
        return $return;
    }
//
}
</pre>
</body>
</html>